package com.asset.dispoal.evidenceconfig.service;

import com.asset.dispoal.evidenceconfig.api.BsEvidenceConfItemDetailService;
import com.asset.dispoal.evidenceconfig.domain.db.entity.BsEvidenceConfItemDetail;
import com.asset.dispoal.evidenceconfig.domain.db.mapper.BsEvidenceConfItemDetailMapper;
import com.asset.dispoal.evidenceconfig.dto.request.BsEvidenceConfItemDetailRequest;
import com.asset.dispoal.evidenceconfig.dto.response.BsEvidenceConfItemDetailResponse;
import com.asset.dispoal.evidenceconfig.service.converter.BsEvidenceConfItemDetailConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.mhuang.pacebox.core.exception.BusinessException;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.core.protocol.page.PageUtil;
import tech.mhuang.pacebox.springboot.core.spring.util.DataUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author qr
 * @date 2021/10/21$
 */
@Service
public class BsEvidenceConfItemDetailServiceImpl implements BsEvidenceConfItemDetailService {

    @Autowired
    BaseIdeable<String> snowFlake;

    @Autowired
    private BsEvidenceConfItemDetailMapper detailMapper;


    @Override
    public PageVO<BsEvidenceConfItemDetailResponse> queryPage(BsEvidenceConfItemDetailRequest bsEvidenceConfItemDetailRequest) {
        PageVO<BsEvidenceConfItemDetailResponse> pageVo = new PageVO<>();
        pageVo.setTotalSize(detailMapper.queryPageCount(bsEvidenceConfItemDetailRequest));
        if (PageUtil.checkPage(pageVo.getTotalSize(), bsEvidenceConfItemDetailRequest)) {
            Page<BsEvidenceConfItemDetailRequest> page = new Page<>();
            page.setRecord(bsEvidenceConfItemDetailRequest);
            page.setStart((bsEvidenceConfItemDetailRequest.getStart() - 1) * bsEvidenceConfItemDetailRequest.getRows());
            page.setRows(bsEvidenceConfItemDetailRequest.getRows());
            final List<BsEvidenceConfItemDetail> bsEvidenceConfItemDetails = detailMapper.queryPage(page);
            final List<BsEvidenceConfItemDetailResponse> bsEvidenceConfItemDetailResponses = BsEvidenceConfItemDetailConverter.INSTANCE.dosTRes(bsEvidenceConfItemDetails);
            pageVo.setResult(bsEvidenceConfItemDetailResponses);
        }
        return pageVo;
    }

    @Override
    public void insertOrUpdate(BsEvidenceConfItemDetailRequest bsEvidenceConfItemDetailRequest) {

        if (bsEvidenceConfItemDetailRequest == null) {
            throw new BusinessException(Result.SYS_FAILD, "参数错误(请求对象为空)");
        }
        BsEvidenceConfItemDetail detail = DataUtil.copyTo(bsEvidenceConfItemDetailRequest, BsEvidenceConfItemDetail.class);
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        final BsEvidenceConfItemDetail byId = detailMapper.getById(detail.getId());
        if (byId == null) {
            //添加
            if (StringUtil.isBlank(detail.getItemId()) || StringUtil.isBlank(detail.getTypeCode()) || StringUtil.isBlank(detail.getTypeName())) {
                throw new BusinessException(Result.SYS_FAILD, "参数错误(分类id/案件证据名称/案件证据)不能为空");
            }
            if (StringUtil.isBlank(detail.getMode()) || StringUtil.isBlank(detail.getMapperId()) || StringUtil.isBlank(detail.getProcessMode())) {
                throw new BusinessException(Result.SYS_FAILD, "参数错误(证据方式、证据项、处理方式)不能为空");
            }
            detail.setId(snowFlake.generateId());
            detail.setCreateUser(userId);
            detail.setCreateTime(new Date());
            detailMapper.insert(detail);
        } else {
            //修改
            BsEvidenceConfItemDetail newBsEvidenceConfItemDetail = new BsEvidenceConfItemDetail();
            DataUtil.copyProperties(byId, newBsEvidenceConfItemDetail, getNullPropertyNames(byId));
            DataUtil.copyProperties(detail, newBsEvidenceConfItemDetail, getNullPropertyNames(detail));
            newBsEvidenceConfItemDetail.setOperateUser(userId);
            newBsEvidenceConfItemDetail.setOperateTime(new Date());
            detailMapper.update(newBsEvidenceConfItemDetail);
        }

    }

    public static String[] getNullPropertyNames(Object source) {
        return BsEvidenceConfServiceImpl.getNullPropertyNames(source);
    }

    @Override
    public List<Map<String, String>> findMapper(String batchId) {
        return detailMapper.findMapper(batchId);
    }

    @Override
    public void batchInsertOrUpdate(List<BsEvidenceConfItemDetailRequest> bsEvidenceConfItemDetailRequests) {
        for (BsEvidenceConfItemDetailRequest bsEvidenceConfItemDetailRequest : bsEvidenceConfItemDetailRequests) {
            insertOrUpdate(bsEvidenceConfItemDetailRequest);
        }
    }

    @Override
    public void delete(String id) {
        detailMapper.delete(id);
    }
}
